package cn.stylefeng.guns.cp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description 生成随机数1-10
 * @Author xiesm
 * @Date 2023/11/2 15:58
 **/
@Slf4j
public class CpExecute {

    private static final Integer num = 270;
    private static final String token = "6fd259fd6368473ca6e0719bb778e9ce1706755512151";
    private static final Integer threadNum = 10;
    private static JSONObject codeJSON = new JSONObject();
    private static List<String> uncode = Lists.newCopyOnWriteArrayList();

    public static void main(String[] args) {
        FileUtil.writeString(1 + "", "d:\\home\\code\\shengc\\version.txt", Charset.defaultCharset());
        for (int i = 0; i < threadNum; i++) {
            ThreadUtil.execute(() -> execute());
        }
    }

    private static AtomicInteger ai = new AtomicInteger(0);


    public static void execute() {
        String path = getPath();
        AtomicInteger tNum = new AtomicInteger(0);
        AtomicInteger fNum = new AtomicInteger(0);
        while (true) {
            Integer ss = Integer.valueOf(DateUtil.format(new Date(), "ss"));
            if (ss < 5 || ss > 15) {
                ThreadUtil.sleep(1000);
            } else if (ss > 10) {
                try {
                    get();
                    schduleWriteCode(path, tNum, fNum);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
                if (ai.get() == threadNum) {
                    ai.set(0);
                    codeJSON.clear();
                    System.out.println("---------------");
                }
                ThreadUtil.sleep(30000);
            }
        }
    }

    public synchronized static Integer print(Boolean boo, String issue, String path, AtomicInteger tNum, AtomicInteger fNum) {
        if (boo) {
            tNum.incrementAndGet();
            System.out.println(StrUtil.format("{},path:{},issue:{},T:{},F:{}", ai.incrementAndGet(), path, issue, tNum, fNum));
        } else {
            fNum.incrementAndGet();
            System.out.println(StrUtil.format("{},path:{},issue:{},T:{},F:{},{}", ai.incrementAndGet(), path, issue, tNum, fNum, false));
        }
        return null;
    }


    public synchronized static String getPath() {
        String version = FileUtil.readString("d:\\home\\code\\shengc\\version.txt", Charset.defaultCharset());
        String path = StrUtil.format("d:\\home\\code\\shengc\\sc10-4-{}.txt", version);
        FileUtil.writeString(Integer.valueOf(version) + 1 + "", "d:\\home\\code\\shengc\\version.txt", Charset.defaultCharset());
        return path;
    }

    public static void schduleWriteCode(String path, AtomicInteger tNum, AtomicInteger fNum) {
//        JSONObject res = get();
        String code = codeJSON.getString("code");
        String issue = codeJSON.getString("issue");
        String[] codeArr = code.split(" ");
        code = StrUtil.format("{} {} {} {}",
                StrUtil.padPre(codeArr[0], 2, "0"),
                StrUtil.padPre(codeArr[1], 2, "0"),
                StrUtil.padPre(codeArr[2], 2, "0"),
                StrUtil.padPre(codeArr[3], 2, "0")
        );
        if (!FileUtil.exist(path)) {
            FileUtil.writeString("", path, Charset.defaultCharset());
        }
        Boolean boo = check(code, path);
//        if (boo) {
//            tNum.incrementAndGet();
//            System.out.println(StrUtil.format("{},path:{},issue:{},T:{},F:{}", getAi(), path, issue, tNum, fNum));
//        } else {
//            fNum.incrementAndGet();
//            System.out.println(StrUtil.format("{},path:{},issue:{},T:{},F:{},{}", getAi(), path, issue, tNum, fNum, false));
//        }
        print(boo, issue, path, tNum, fNum);
        shengcheng(code, path);
    }

    public synchronized static void get() {
        if (CollUtil.isEmpty(codeJSON)) {
            JSONObject jsonObject = ScUtil.sendSc(token, "48", "1", 1);
            JSONObject data = jsonObject.getJSONArray("data").getJSONObject(0);
            String code = data.getString("code");
            String issue = data.getString("issue");
            codeJSON.put("code", code);
            codeJSON.put("issue", issue);
            System.out.println(codeJSON);
        }
    }

    public static String getCode(String code) {
        String[] codes = code.split("\\||");
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < codes.length; i++) {
            if (StrUtil.equalsIgnoreCase("0", codes[i])) {
                list.add("10");
            } else {
                list.add(StrUtil.padPre(codes[i], 2, "0"));
            }
        }
        return CollUtil.join(list, " ");
    }

    public static Boolean check(String code, String path) {
        List<String> lists = FileUtil.readLines(path, Charset.defaultCharset());
        boolean boo = lists.contains(code);
        return boo;
    }

    public static void shengcheng(String code, String path) {
        while (true) {
            List<String> lists = random();
            if (!lists.contains(code)) {
                FileUtil.writeLines(lists, path, Charset.defaultCharset());
                break;
            }
        }

    }

    public static List<String> random() {
        List<String> lists = Lists.newArrayList();
        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= 10; j++) {
                if (i == j) {
                    continue;
                }
                for (int k = 1; k <= 10; k++) {
                    if (j == k || i == k) {
                        continue;
                    }
                    for (int l = 1; l <= 10; l++) {
                        if (k == l || j == l || i == l) {
                            continue;
                        }
                        String s1 = StrUtil.padPre(i + "", 2, "0");
                        String s2 = StrUtil.padPre(j + "", 2, "0");
                        String s3 = StrUtil.padPre(k + "", 2, "0");
                        String s4 = StrUtil.padPre(l + "", 2, "0");
                        String code = StrUtil.format("{} {} {} {}", s1, s2, s3, s4);
                        int random = RandomUtil.randomInt(1, 5040);
                        if (random > num) {
                            lists.add(code);
                        } else {
//                            System.out.println(random + ":" + code);
//                            add(code);
                        }
                    }
                }
            }
        }
        return lists;
    }

    public static synchronized void add(String code) {
        uncode.add(code);
    }
}